'use strict';

let baseService =require('./baseService.js');

class mannagerService extends baseService {
   async list() {
       const {service,model} = this.ctx;

       let listResult=await this.ctx.model.Admin.aggregate([{
           $lookup:{
               from:'role',
               localField:'role_id',
               foreignField:'_id',
               as:'role'

           }
       }])

        return listResult
    }
    
    async doAdd(bodyData) {
        bodyData.password=await this.service.tools.md5(bodyData.password);
        //判断当前用户是否存在
        let adminResult=await this.ctx.model.Admin.find({"username":bodyData.username});
        console.log("获得的数组");
        console.log(adminResult);
        if(adminResult.length>0){
           return 0
        }else{
            let  admin=new this.ctx.model.Admin(bodyData);
            let addResult=await admin.save();
            console.log("添加的结果");
            console.log(addResult);
            if(addResult.status){
                return 1
            }else{
                return 0
            }
        }
    }
    async doEdit(editId) {
        const {service,model} = this.ctx;
        return await this.ctx.model.Role.find();
    }
    async findById(findId) {
        const {service,model} = this.ctx;
        let findResult=await model.Admin.find({"_id":findId});

        return findResult
    }
}

module.exports =mannagerService;

